package ua.pp.bizon.cripto.configuration;

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Arrays;

import javax.swing.JOptionPane;

import ua.pp.bizon.cripto.authorization.CashedUI;
import ua.pp.bizon.cripto.authorization.Credentials;
import ua.pp.bizon.cripto.authorization.UI;
import ua.pp.bizon.cripto.file.FileFactory;
import ua.pp.bizon.cripto.gui.MainFrame;
import ua.pp.bizon.cripto.keystore.CryptoException;
import ua.pp.bizon.cripto.keystore.CryptoUtil;
import ua.pp.bizon.cripto.keystore.CryptoUtil.Direction;
import ua.pp.bizon.cripto.utils.ExceptionHandler;

public class MainAppConsole implements UI, ExceptionHandler {

    /**
     * @param args
     */
    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        System.out.println(Arrays.asList(args));
        try {
            if (args.length == 1 && args[0].equals("--gui")) {
                new MainFrame().setVisible(true);
            } else {
                ConfigurationFactory factory = new ConfigurationFactory();
                factory.configure(args);
                Configuration c = factory.getConfiguration();
                if (c == null) {
                    System.err.println("error: no configuration");
                    System.err.println("default configuration is in file config.properties");
                    System.err.println("parameters from command line:");
                    System.err.println("from=file or ftp url");
                    System.err.println("read_config=other config file");
                    System.err.println("to=file or ftp url");
                    System.err.println("ftp=path to ftp config (if needed)");
                    System.err.println("overwrite= true or false");
                    System.err.println("direction=encode or direction=decode");
                }

                CryptoUtil cryptoUtil = new CryptoUtil(Credentials.init(new CashedUI(new MainAppConsole())));

                cryptoUtil.process(c);
            }
        } catch (Exception e) {
            new MainAppConsole().handle(e);
        }
    }

    public void handle(Exception e) {
        System.err.println(e.getClass() + " " + e.getMessage());
        e.printStackTrace();
    }

    @Override
    public void login(Credentials credentials) throws IOException, CryptoException {
        System.out.println("Input login:");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        credentials.setUsername(bufferedReader.readLine());
        Console cons = System.console();
        char[] passwd = null;
        if (cons != null) {
            passwd = cons.readPassword("[%s]", "Password:");
        } else {
            System.out.println("Input password(it is not hidden!):");
            passwd = bufferedReader.readLine().toCharArray();
        }
        credentials.setHashedPassword(CryptoUtil.cryptPassword(passwd));
        System.out.println("login=" + credentials.getUsername());
        System.out.println("hash=" + credentials.getHashedPassword());

    }

    @Override
    public Configuration configure(Configuration c) {
        try {
            if (c == null) {
                c = new Configuration();
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            if (c.getFrom() == null) {
                System.out.println("Input from:");
                c.setFrom(FileFactory.parse(reader.readLine()));            
            }
            if (c.getTo() == null) {
                System.out.println("Input to:");
                c.setTo(FileFactory.parse(reader.readLine()));
            }
            while (c.getDirection() == null){
                System.out.println("Input direction:");
                c.setDirection(Direction.read(reader.readLine()));
            }
        } catch (IOException e) {
            handle(e);
        }
        return c;
    }
    
    @Override
    public void done() {
        System.out.println("done");
    }

}
